//
//  GzzFhSubController.m
//  Estate
//
//  Created by heyong on 16/7/14.
//  Copyright © 2016年 ttsx. All rights reserved.
//

#import "GzzFhSubController.h"
#import "addBtnCell.h"
#import "B_addFhCell.h"
@interface GzzFhSubController ()
{
    NSMutableArray *_showKeyArr;

}
@end

@implementation GzzFhSubController

//懒加载
- (NSMutableArray *)dataArray{
    if(_dataArray==nil){
        _dataArray=[NSMutableArray array];
        
    }
    return  _dataArray;
}

- (TPKeyboardAvoidingTableView *)tableView{
    if(_tableView==nil){
        _tableView=[[TPKeyboardAvoidingTableView alloc]initWithFrame:CGRECTS(0, 0, 375, 667)];
        _tableView.dataSource=self;
        _tableView.delegate=self;
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        _tableView.tableFooterView=[[UIView alloc]init];
        [_tableView registerClass:[B_addFhCell class] forCellReuseIdentifier:@"addCell"];
        [_tableView registerClass:[addBtnCell class] forCellReuseIdentifier:@"addbtn"];
    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"旁站-混泥土灌注桩-物料信息";
    self.view.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.tableView];
    self.num=0;
    //添加右上方提交按钮
    if(self.isEdit){
        self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"wancheng"] style:UIBarButtonItemStyleDone target:self action:@selector(clickSave)];

    }
    [self setData];

}

- (void)clickSave{
    if(_site){
        NSMutableArray * passOnArr=[NSMutableArray array];
        for (int i=0; i<self.dataArray.count; i++) {
            //模型转字典
            NSDictionary *dic = [self.dataArray[i] yy_modelToJSONObject];
            [passOnArr addObject:dic];
        }
        NSMutableDictionary *dics=[NSMutableDictionary dictionary];
        for(int i=0;i<passOnArr.count;i++){
            NSMutableDictionary *tempDic=[NSMutableDictionary dictionary];
            NSDictionary *dic=[passOnArr objectAtIndex:i];
            [tempDic setObject:dic[@"zhuh"] forKey:@"桩号"];
            [tempDic setObject:dic[@"zdbg"] forKey:@"桩底标高"];
            [tempDic setObject:dic[@"czhd"] forKey:@"沉渣厚度"];
            [tempDic setObject:dic[@"zwbc"] forKey:@"桩位偏差"];
            [tempDic setObject:dic[@"czdpc"] forKey:@"垂直度偏差"];
            [tempDic setObject:dic[@"zdbgao"] forKey:@"桩顶标高"];
            [tempDic setObject:dic[@"hcgqk"] forKey:@"砼超灌情况"];
            [tempDic setObject:dic[@"yic"] forKey:@"异常处理情况"];
            [tempDic setObject:dic[@"qita"] forKey:@"其他"];

            [dics setObject:tempDic forKey:dic[@"number"]];
        }
        self.dataBlock(dics);
        
    }else{
        
        NSMutableArray * passOnArr=[NSMutableArray array];
        
        for (int i=0; i<self.dataArray.count; i++) {
            //模型转字典
            NSDictionary *dic = [self.dataArray[i] yy_modelToJSONObject];
            
            [passOnArr addObject:dic];
        }
        
        NSMutableDictionary *dics=[NSMutableDictionary dictionary];
        
        for(int i=0;i<passOnArr.count;i++){
            NSMutableDictionary *tempDic=[NSMutableDictionary dictionary];
            NSDictionary *dic=[passOnArr objectAtIndex:i];
            [tempDic setObject:dic[@"zhuh"] forKey:@"桩号"];
            [tempDic setObject:dic[@"zdbg"] forKey:@"桩底标高"];
            [tempDic setObject:dic[@"czhd"] forKey:@"沉渣厚度"];
            [tempDic setObject:dic[@"zwbc"] forKey:@"桩位偏差"];
            [tempDic setObject:dic[@"czdpc"] forKey:@"垂直度偏差"];
            [tempDic setObject:dic[@"zdbgao"] forKey:@"桩顶标高"];
            [tempDic setObject:dic[@"hcgqk"] forKey:@"砼超灌情况"];
            [tempDic setObject:dic[@"yic"] forKey:@"异常处理情况"];
            [tempDic setObject:dic[@"qita"] forKey:@"其他"];
            [dics setObject:tempDic forKey:dic[@"number"]];
        }
        
        self.dataBlock(dics);
    }

    [self.navigationController popViewControllerAnimated:YES];
}

- (void) setData{
    WEAKSELF
    //查看页
    if(_site){
        _showKeyArr=[NSMutableArray array];
        //循环遍历、找到显示的视图。
        for(int i=1;i<=10;i++){
            NSString *key=[NSString stringWithFormat:@"序号%d是否显示",i];
            NSString *value=[_site.fh_hnt objectForKey:key];
            //存入所有要显示的key
            if([value isEqualToString:@"是"]){
                [_showKeyArr addObject:[NSString stringWithFormat:@"序号%d",i]];
            }
        }
        //取到显示视图的值 根据显示的key
        for (NSString *key in _showKeyArr) {
            site_05_fh *site=[site_05_fh siteWithDict:_site.fh_hnt[key] AndNumber:key];
            [weakSelf.dataArray addObject:site];
        }
    }

}

- (void)addDataDefaine:(long)l{
    NSDictionary *dic=@{@"桩号":@"",@"桩底标高":@"",@"沉渣厚度":@"",@"桩位偏差":@"",@"垂直度偏差":@"",@"桩顶标高":@"",@"砼超灌情况":@"",@"异常处理情况":@"",@"其他":@""};
    site_05_fh *site=[site_05_fh siteWithDict:dic AndNumber:[NSString stringWithFormat:@"序号%li",l]];
    [self.dataArray addObject:site];

}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //最后一行 添加添加按钮
    if(indexPath.row==self.dataArray.count){
        return 40*BIZ;
    }
    return 550;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count+1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WEAKSELF
    //显示添加按钮
    if(indexPath.row==self.dataArray.count){
        addBtnCell *cell=[tableView dequeueReusableCellWithIdentifier:@"addbtn" forIndexPath:indexPath];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        //添加视图
        cell.addBlock=^(){
            [weakSelf addDataDefaine:self.num+1];
            [weakSelf.tableView reloadData];
            
            //            // 滚动到新增指定的区域
            [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.dataArray.count-1 inSection:0]
                                  atScrollPosition:UITableViewScrollPositionTop animated:YES];
        };
        return cell;
    }else{//显示视图
        
        B_addFhCell *cell=[tableView dequeueReusableCellWithIdentifier:@"addCell" forIndexPath:indexPath];
        
        //第一行隐藏 删除按钮
        if(indexPath.row==0){
            cell.dleBtn.hidden=YES;
        }else{
            cell.dleBtn.hidden=NO;
        }
        
        //设置数据
        site_05_fh *siteDetail=[self.dataArray objectAtIndex:indexPath.row];
        cell.site=siteDetail;
        
        //设置标题
        NSArray *numStr=[siteDetail.number componentsSeparatedByString:@"号"];
        self.num=[[numStr lastObject]integerValue];
        
        //设置序号
        cell.titLab.text=siteDetail.number;
        
        //设置数据
        cell.text1Block=^(NSString *str){
            siteDetail.zhuh=str;
        };
        
        cell.text2Block=^(NSString *str){
            siteDetail.zdbg=str;
        };
        
        cell.text3Block=^(NSString *str){
            siteDetail.czhd=str;
        };
        
        cell.text4Block=^(NSString *str){
            siteDetail.zwbc=str;
        };
        
        cell.text5Block=^(NSString *str){
            siteDetail.czdpc=str;
        };
        
        cell.text6Block=^(NSString *str){
            siteDetail.zdbgao=str;
        };
        
        cell.text7Block=^(NSString *str){
            siteDetail.hcgqk=str;
        };
        
        cell.text8Block=^(NSString *str){
            siteDetail.yic=str;
        };
        cell.text9Block=^(NSString *str){
            siteDetail.qita=str;
        };
        
        //移除当前行数
        cell.deleteBlock=^(){
            
            [weakSelf.dataArray removeObjectAtIndex:indexPath.row];
            
            [weakSelf.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationFade)];
            
            [weakSelf.tableView reloadData];
            
        };
        
        return cell;
    }
}



#pragma mark scrollview
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    if (scrollView == self.tableView) {
        [self.view endEditing:YES];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
